//
//  BLClothRecicalPresentViewController.m
//  blanket
//
//  Created by Mac on 2017/12/6.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BLClothRecicalPresentViewController.h"
#import "BLTextFieldControl.h"
@interface BLClothRecicalPresentViewController ()
@property(nonatomic, strong) STButton                     *clearButton;
@property(nonatomic, strong) UIView                     *customView;
@property(nonatomic, strong) BLButtonControl                     *contactControl;
@property(nonatomic, strong) BLButtonControl                     *phoneControl;
@property(nonatomic, strong) BLButtonControl                     *numControl;
@property(nonatomic, strong) BLButtonControl                     *nameControl;
@property(nonatomic, strong) BLButtonControl                     *addControl;

@end

@implementation BLClothRecicalPresentViewController
- (instancetype)init
{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    return self;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self show];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.clearButton = [[STButton alloc] initWithFrame:UIScreenFrame
                                                 title:nil
                                            titleColor:nil
                                             titleFont:0
                                          cornerRadius:0
                                       backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                       backgroundImage:nil
                                                 image:nil];
    [weakSelf.clearButton setClicAction:^(UIButton *sender) {
        [weakSelf dismiss];
    }];
    [self.view addSubview:_clearButton];
    
    self.customView = [[UIView alloc] initWithFrame:CGRectMake(0, UIScreenHeight, UIScreenWidth - 60, 342)];
    self.customView.centerX = UIScreenWidth / 2;
    self.customView.top = UIScreenHeight;
    self.customView.backgroundColor = [UIColor whiteColor];
    self.customView.layer.cornerRadius = 10;
    self.customView.clipsToBounds = YES;
    [self.clearButton addSubview:self.customView];
    UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectedWhiteView)];
    self.customView.userInteractionEnabled = YES;
    [self.customView addGestureRecognizer:ges];
   
    //sub
    self.contactControl = [[BLButtonControl alloc] initWithLineFrame:CGRectMake(20, 20, self.customView.width - 40, 44) leftTitle:@"联系人" rightTitle:@"" leftImageName:@"联系人"];
    self.contactControl.textFiled.keyboardType = UIKeyboardTypeDefault;
    [self.customView addSubview:self.contactControl];
    
    self.phoneControl = [[BLButtonControl alloc] initWithLineFrame:CGRectMake(20, 0 + _contactControl.bottom, self.customView.width - 40, 44) leftTitle:@"联系电话" rightTitle:@"" leftImageName:@"手机1"];
    [self.customView addSubview:self.phoneControl];
    
    self.numControl = [[BLButtonControl alloc] initWithLineFrame:CGRectMake(20, 0 + _phoneControl.bottom, self.customView.width - 40, 44) leftTitle:@"酒店规模" rightTitle:@"床位" leftImageName:@"导航栏-对比-APP规模"];
    [self.customView addSubview:self.numControl];
    
    self.nameControl = [[BLButtonControl alloc] initWithLineFrame:CGRectMake(20, 0 + _numControl.bottom, self.customView.width - 40, 44) leftTitle:@"酒店名称" rightTitle:@"" leftImageName:@"名称"];
     self.nameControl.textFiled.keyboardType = UIKeyboardTypeDefault;
    [self.customView addSubview:self.nameControl];
    
    self.addControl = [[BLButtonControl alloc] initWithLineFrame:CGRectMake(20, 0 + _nameControl.bottom, self.customView.width - 40, 44) leftTitle:@"酒店地址" rightTitle:@"" leftImageName:@"地址"];
     self.addControl.textFiled.keyboardType = UIKeyboardTypeDefault;
    [self.customView addSubview:self.addControl];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, self.addControl.bottom +10, self.customView.width, 20)
                                                     text:@"我们将3~5天对您进行线下拜访"
                                                textColor:BL_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.attributedText = [titleLable.text st_convertAttributeStringWithKeyWord:@"3~5" attributes:@{NSForegroundColorAttributeName:BL_redColor}];
    [self.customView addSubview:titleLable];
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(60, titleLable.bottom + 15, self.customView.width - 120, 44)
                                                        title:@"确定"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:5
                                              backgroundColor:BL_BlueBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];
    
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.customView addSubview:confimButton];
    
    self.customView.height = confimButton.bottom + 20;
    
}

#pragma mark --Private Method
- (void)show{
    [UIView animateWithDuration:0.6
                          delay:0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.7
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
                         self.customView.centerY = UIScreenHeight / 2 - 40;
                     } completion:^(BOOL finished) {
                         
                     }];
}
- (void)dismiss{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}
#pragma mark --Action Method
- (void)onSelectedCommitButton{
    NSString * json = self.anwserArray.mj_JSONString;
    if (self.isHotolShare) {
        //如果是来源是酒店回收
        self.dic = [NSMutableDictionary new];
        
    }
    if ([STUserManger defult].userID) {
        [self.dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    [self.dic setObject:debugToken forKey:@"token"];
    //回收
    if (!self.contactControl.textFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"您还未输入联系人"];
        return;
    }else{
        [self.dic setObject:self.contactControl.textFiled.text forKey:@"username"];
    }
    
    if (!self.phoneControl.textFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"您还未输入联系方式"];
        return;
    }else{
        [self.dic setObject:self.phoneControl.textFiled.text forKey:@"mobile"];
    }
    
    if (!self.numControl.textFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"您还未输入酒店规模"];
        return;
    }else{
        [self.dic setObject:self.numControl.textFiled.text forKey:@"hotel_scale"];
        if (self.isHotolShare) {
            [self.dic setObject:self.numControl.textFiled.text forKey:@"bed"];
        }else{
              [self.dic setObject:self.numControl.textFiled.text forKey:@"bed_num"];
        }
      
    }
    
    if (!self.nameControl.textFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"您还未输入酒店名称"];
        return;
    }else{
        [self.dic setObject:self.nameControl.textFiled.text forKey:@"hotel_name"];
    }
    
    if (!self.addControl.textFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"您还未输入酒店地址"];
        return;
    }else{
        [self.dic setObject:self.addControl.textFiled.text forKey:@"hotel_address"];
    }
    
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/recycleBinAction"];

    if (self.isHotolShare) {
        url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/submitProblemAction"];
        //增加json
        if (json.length) {
            [self.dic setObject:json forKey:@"json"];
        }
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:self.dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
     
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 //NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 [SVProgressHUD showSuccessWithStatus:@"提交成功，我们将在3~5天对您拜访"];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {

                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
    
}
- (void)onSelectedWhiteView{
    [self.view endEditing:YES];
}
@end
